#!/usr/bin/env python
#coding=utf-8
from flask import render_template,url_for,redirect,request
from Applications.libs.redprint import Redprint
from Applications.libs.utils import reluesInfo
from Applications.forms import AdminsForms
from Applications.libs import jwt
from Applications.services.AdminService import adminService

Auth_redprint = Redprint('admin/auth')

@Auth_redprint.route('/getoken',methods=['post'])
def getoken():
    if request.method == 'POST':
        form = AdminsForms.admin_login()
        if not form.validate_on_submit():
            return reluesInfo(status=400,relues=form.errors).dumps(),400

        relust, admin_info, admin_menu = adminService.admin_login(form.admin_name.data, form.admin_passwd.data)

        if admin_info:
            import json
            admin_info.uid = admin_info.admin_id
            access_token, refresh_token = jwt.get_tokens(user=admin_info,verify_remote_addr=True)
            tokens = {'access_token':access_token,'refresh_token':refresh_token}

            relust = reluesInfo(status=200,relues=tokens)
            return relust.dumps()

        return relust.dumps()
